package org.pay.service.impl;

import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.pay.core.entity.MchPassage;
import org.pay.core.entity.MchPassageExample;
import org.pay.core.service.IMchPassageService;
import org.pay.service.dao.mapper.MchPassageMapper;

import java.util.List;

/**
 * @author: yangshancheng
 * @date: 18/2/11
 * @description:
 */
@Service
public class MchPassageServiceImpl implements IMchPassageService {

    @Autowired
    private MchPassageMapper mchPassageMapper;

    @Override
    public int add(MchPassage mchPassage) {
        return mchPassageMapper.insertSelective(mchPassage);
    }

    @Override
    public int update(MchPassage mchPassage) {
        return mchPassageMapper.updateByPrimaryKeySelective(mchPassage);
    }

    @Override
    public MchPassage findById(Integer id) {
        return mchPassageMapper.selectByPrimaryKey(id);
    }

    @Override
    public MchPassage findByMchIdAndPassageId(Long mchId, Integer passageId) {
        MchPassageExample example = new MchPassageExample();
        MchPassageExample.Criteria criteria = example.createCriteria();
        criteria.andMchIdEqualTo(mchId);
        criteria.andPassageIdEqualTo(passageId);
        List<MchPassage> mchPassageList = mchPassageMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(mchPassageList)) return null;
        return mchPassageList.get(0);
    }

    @Override
    public List<MchPassage> select(int offset, int limit, MchPassage mchPassage) {
        MchPassageExample example = new MchPassageExample();
        example.setOffset(offset);
        example.setLimit(limit);
        MchPassageExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, mchPassage);
        return mchPassageMapper.selectByExample(example);
    }

    @Override
    public List<MchPassage> selectAll(MchPassage mchPassage) {
        MchPassageExample example = new MchPassageExample();
        MchPassageExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, mchPassage);
        return mchPassageMapper.selectByExample(example);
    }

    @Override
    public Integer count(MchPassage mchPassage) {
        MchPassageExample example = new MchPassageExample();
        MchPassageExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, mchPassage);
        return mchPassageMapper.countByExample(example);
    }

    void setCriteria(MchPassageExample.Criteria criteria, MchPassage mchPassage) {
        if(mchPassage != null) {
            if(mchPassage.getMchId() != null) criteria.andMchIdEqualTo(mchPassage.getMchId());
            if(mchPassage.getPassageId() != null) criteria.andPassageIdEqualTo(mchPassage.getPassageId());
        }
    }
}
